package com.yuexunit.operationman.nbsy.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexunit.operationman.nbsy.R;

import org.kymjs.kjframe.ui.ViewInject;


/**
 * Created by chenss on 2017/9/7 0007.
 */

public class FlaxMeterDialog {

    private Context context;
    private Dialog dialog;
    private Display display;

    TextView titleView;
    TextView timeView;
    EditText content;
    LinearLayout bp_view;
    EditText dbpView;
    EditText sbpView;
    Button sure;
    Button cancel;

    boolean isBp;
    int max;
    private ICallBack icb;

    public FlaxMeterDialog(Context context, boolean isBp) {
        this.context = context;
        this.isBp = isBp;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public FlaxMeterDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_flaxmeter, null);

        titleView = (TextView)view.findViewById(R.id.title);
        timeView = (TextView)view.findViewById(R.id.time);
        bp_view = (LinearLayout)view.findViewById(R.id.bp_view);
        content = (EditText)view.findViewById(R.id.content);
        dbpView = (EditText)view.findViewById(R.id.dbp);
        sbpView = (EditText)view.findViewById(R.id.sbp);
        cancel = (Button)view.findViewById(R.id.cancel);
        sure = (Button)view.findViewById(R.id.sure);

        if(isBp){
            content.setVisibility(View.GONE);
            bp_view.setVisibility(View.VISIBLE);
        }else{
            content.setVisibility(View.VISIBLE);
            bp_view.setVisibility(View.GONE);
        }

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(icb != null){
                    Bundle bundle = new Bundle();
                    if(isBp){
                        bundle.putString("dbp", dbpView.getText().toString());
                        bundle.putString("sbp", sbpView.getText().toString());
                    }else{
                        bundle.putString("content", content.getText().toString());
                    }
                    icb.callback(bundle);
                }
            }
        });

        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!content.getText().toString().isEmpty() && Integer.valueOf(content.getText().toString()) > max){
                    ViewInject.toast("输入数据格式不正确");
                    return;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.loadingDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public FlaxMeterDialog setTitle(String title, String time) {
        titleView.setText(title);
        timeView.setText(time);
        content.setHint(title);
        return this;
    }

    public FlaxMeterDialog setDtata(String data, int max) {
        this.max = max;
        content.setText(data);
        return this;
    }

    public FlaxMeterDialog setBp(String sbp, String dbp) {
        sbpView.setText(sbp);
        dbpView.setText(dbp);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    /**
     * 三。设置回调变量
     * @param cb
     */
    // 如果不是使用上面接收icallback参数的(二)构造方法创建此自定义对话框，则需要使用此方法指定icb变量
    public void setCallBack (ICallBack cb) {
        icb = cb;
    }

    public interface ICallBack {
        void callback(Bundle bundle);
    }
}
